package com.xxx.video.dynamic.activitys;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.gson.Gson;
import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.utils.AppUtils;
import com.xxx.video.commen.utils.ClientInfoUtils;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.LocationUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.dynamic.adapters.PeopleNearbyVerticalListAdapter;
import com.xxx.video.dynamic.beans.FriendListBean;
import com.xxx.video.dynamic.presenters.IPeopleNearbyView;
import com.xxx.video.dynamic.presenters.PeopleNearbyPresenter;

import butterknife.BindView;


public class PeopleNearbyActivity extends BaseActivity<PeopleNearbyPresenter> implements IPeopleNearbyView,
        RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener, BaseRecycleViewAdapter.OnItemClickListener<FriendListBean.PageBean.ListBean> {
    @BindView(R.id.rcv_people_nearby_list)
    RefreshRecyclerView rcvPeopleNearbyList;
    /**
     * location
     */
    private Location mLocation;
    /**
     * page
     */
    private int page = 1;
    /**
     * request permission
     */
    public static final int REQUEST_PERMISSION_CODE = 200;
    String[] permissions = {
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.ACCESS_COARSE_LOCATION"
    };
    /**
     * adapter
     */
    private PeopleNearbyVerticalListAdapter mPeopleNearbyVerticalListAdapter;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.title_people_nearby_activity);
    }

    @Override
    protected PeopleNearbyPresenter createPresenter() {
        return new PeopleNearbyPresenter(this, this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_people_nearby;
    }

    @Override
    public void initData(Bundle extras) {
        showRootLayoutStatus(1);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            showRootLayoutStatus(3);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // You can directly ask for the permission.
                requestPermissions(permissions, REQUEST_PERMISSION_CODE);
            }
        } else {
            initList();
            initLocation();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_PERMISSION_CODE:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    initLocation();
                } else {
                    ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_please_open_the_location_permission));
                }
                return;
            default:
                break;
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        rcvPeopleNearbyList.setLayoutManager(new LinearLayoutManager(this));
        mPeopleNearbyVerticalListAdapter = new PeopleNearbyVerticalListAdapter(this);
        rcvPeopleNearbyList.setAdapter(mPeopleNearbyVerticalListAdapter);
        rcvPeopleNearbyList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        mPeopleNearbyVerticalListAdapter.setOnItemClickListener(this);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void initLocation() {
        LocationUtils.getLocation(this, (OnSuccessListener<Location>) location -> {
            if (location != null) {
                mLocation = location;
                ClientInfoUtils.getInstance().updateClientInfo(this);
                Log.d("location",new Gson().toJson(mLocation.toString()));
                AppUtils.getInstance().isWifi();
                getPresenter().nearbyUser(String.valueOf(page), String.valueOf(location.getLongitude()), String.valueOf(location.getLatitude()));
            } else {
                showRootLayoutStatus(3);
            }
        });
    }

    @Override
    public void startPersonInfoActivity(String userId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", userId);
        intent2Activity(PersonInfoActivity.class, bundle);
    }

    @Override
    public void initNearByFriendList(FriendListBean.PageBean pageBean) {
        rcvPeopleNearbyList.resetState();
        showLayoutStatus(1);
        if (page == 1) {
            mPeopleNearbyVerticalListAdapter.setData(pageBean.list);
            if (ListUtils.isEmpty(pageBean.list)) {
                showLayoutStatus(2);
            }
        } else {
            mPeopleNearbyVerticalListAdapter.addData(pageBean.list);
        }
        if (!ListUtils.isEmpty(pageBean.list) && pageBean.list.size() < 10) {
            rcvPeopleNearbyList.setNoMoreData();
        }
    }

    @Override
    public void addFriendSuccess(int position) {
        if (mPeopleNearbyVerticalListAdapter != null) {
            mPeopleNearbyVerticalListAdapter.addFriendSuccess(position);
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        if (mLocation != null) {
            getPresenter().nearbyUser(String.valueOf(page), String.valueOf(mLocation.getLongitude()), String.valueOf(mLocation.getLatitude()));
        } else {
            showRootLayoutStatus(3);
        }
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        if (mLocation != null) {
            getPresenter().nearbyUser(String.valueOf(page), String.valueOf(mLocation.getLongitude()), String.valueOf(mLocation.getLatitude()));
        } else {
            showRootLayoutStatus(3);
        }
    }

    @Override
    public void onItemClick(FriendListBean.PageBean.ListBean listBean, int position, int type) {
        if (type == 1) {
            getPresenter().addFriends(listBean, position);
        } else {
            startPersonInfoActivity(listBean.userId);
        }
    }
}
